package com.nkym.wypan.controller;

import cn.hutool.core.text.StrSplitter;
import cn.hutool.core.util.StrUtil;
import com.nkym.wypan.constants.FileConstants;
import com.nkym.wypan.enums.FileDelFlag;
import com.nkym.wypan.model.context.userfile.*;
import com.nkym.wypan.model.req.userfile.*;
import com.nkym.wypan.model.resp.userfile.BreadcrumbVo;
import com.nkym.wypan.model.resp.userfile.PageQueryUserFileResp;
import com.nkym.wypan.model.resp.userfile.SearchVo;
import com.nkym.wypan.response.BaseResponse;
import com.nkym.wypan.service.UserFileService;
import com.nkym.wypan.utils.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 19637
 * @description 针对表【wy_pan_user_file】的数据库操作Mapper
 * @createDate 2023-06-07 18:38:09
 * @Entity generator.domain.UserFile
 */
@RestController
@Api(tags = "用户文件模块")
@RequestMapping("/user-file")
public class UserFileController {

    @Resource
    UserFileService userFileService;

    @GetMapping("/page-list")
    @ApiOperation("列表查询当前目录下的所有文件")
    public BaseResponse<PageQueryUserFileResp> list(@Valid QueryUserFileReq req) {
        QueryUserFileContext context = new QueryUserFileContext();
        Long uid = UserContextHolder.getId();
        Integer pageSize = req.getPageSize();
        Integer pageNow = req.getPageNow();
        context.setOffset((pageNow - 1) * pageSize);
        context.setSize(pageSize);
        context.setParentId(req.getParentId());
        context.setUserId(uid);
        context.setDelFlag(FileDelFlag.NORMAL.getCode());
        PageQueryUserFileResp resp = userFileService.list(context);
        return BaseResponse.ok(resp);
    }

    @PostMapping("/add-folder")
    @ApiOperation("创建新文件夹")
    public BaseResponse addFolder(@Valid AddUserFolderReq req) {

        Long parentId = req.getParentId();
        String filename = req.getFilename();

        Long uid = UserContextHolder.getId();
        AddUserFolderContext context = new AddUserFolderContext();
        context.setUserId(uid);
        context.setParentId(parentId);
        context.setFilename(filename);
        userFileService.addFolder(context);
        return BaseResponse.ok();
    }


    @PostMapping("/rename-file")
    @ApiOperation(
            value = "重命名文件",
            notes = "提供文件重命名服务"
    )
    public BaseResponse renameFile(@Valid
                                   @RequestBody
                                   FileRenameReq req) {
        Long uid = UserContextHolder.getId();
        FileRenameContext context = new FileRenameContext();
        context.setFileId(req.getFileId());
        context.setNewFilename(req.getNewFilename());
        context.setUserId(uid);
        userFileService.renameFile(context);
        return BaseResponse.ok();
    }

    @PostMapping("/recycle-file")
    @ApiOperation(
            value = "回收文件",
            notes = "提供文件回收服务,只是放入回收站，不是彻底删除"
    )
    public BaseResponse recycleFile(@Valid
                                    @RequestBody
                                    RecycleReq req) {
        String fids = req.getFileIds();
        Long userId = UserContextHolder.getId();

        List<Long> fileIds = StrSplitter.split(fids, ",", 0, true, true)
                .stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());


        FileDeleteContext fileDeleteContext = new FileDeleteContext();
        fileDeleteContext.setUserId(userId);
        fileDeleteContext.setFileIds(fileIds);
        userFileService.recycleFile(fileDeleteContext);
        return BaseResponse.ok();
    }

    @PostMapping("/transfer-file")
    @ApiOperation(
            value = "转移文件",
            notes = "提供文件转移服务"
    )
    public BaseResponse transferFile(@Valid
                                     @RequestBody
                                     FileTransferReq req) {
        Long uid = UserContextHolder.getId();
        Long targetFileId = req.getTargetFileId();
        String fileIds = req.getFileIds();
        List<String> split = StrSplitter.split(fileIds, FileConstants.SEPARATOR, true, true);
        List<Long> sourceFileId = split.stream().map(Long::parseLong).collect(Collectors.toList());
        FileTransferContext context = new FileTransferContext();
        context.setFileIds(sourceFileId);
        context.setTargetFileId(targetFileId);
        context.setUserId(uid);
        userFileService.transferFile(context);
        return BaseResponse.ok();
    }

    @PostMapping("/search-file")
    @ApiOperation(
            value = "搜索文件",
            notes = "提供文件搜索服务"
    )
    public BaseResponse<List<SearchVo>> searchFile(@Valid
                                                   @RequestBody
                                                   SearchReq searchReq) {
        String keyword = searchReq.getKeyword();
        Integer filetype = searchReq.getFiletype();
        Integer pageNow = searchReq.getPageNow();
        Integer pageSize = searchReq.getPageSize();
        Long uid = UserContextHolder.getId();
        SearchContext searchContext = new SearchContext();
        searchContext.setUserId(uid);
        searchContext.setKeyword("");
        searchContext.setFiletype(0);
        searchContext.setOffset((pageNow - 1) * pageSize);
        searchContext.setSize(pageSize);
        if (StrUtil.isNotBlank(keyword)) {
            searchContext.setKeyword(keyword);
        }
        if (filetype != null && filetype > 0) {
            searchContext.setFiletype(filetype);
        }
        List<SearchVo> res = userFileService.searchFile(searchContext);
        return BaseResponse.ok(res);
    }

    @GetMapping("/breadcrumb")
    @ApiOperation(
            value = "获取面包屑",
            notes = "提供文获取面包屑服务"
    )
    public BaseResponse<List<BreadcrumbVo>> breadcrumb(@Valid
                                                       BreadcrumbReq searchReq) {
        Long fileId = searchReq.getFileId();
        Long userId = UserContextHolder.getId();
        BreadcrumbContext breadcrumbContext = new BreadcrumbContext();
        breadcrumbContext.setFileId(fileId);
        breadcrumbContext.setUserId(userId);

        List<BreadcrumbVo> res =   userFileService.breadcrumb(breadcrumbContext);
        return BaseResponse.ok(res);
    }


}




